//
//  MyCalendarView.swift
//  skincare
//
//  Created by whu125 on 2023/5/23.
//

import FSCalendar
import SwiftUI

struct MyCalendarView: UIViewRepresentable {
    
    let calendar = FSCalendar()
    var onDateSelected: ((Date) -> Void)?
    
    func makeUIView(context: Context) -> FSCalendar {
        
        // Customize the calendar appearance
        calendar.scrollDirection = .vertical
        calendar.locale = Locale(identifier: "en")
        calendar.appearance.eventDefaultColor = UIColor(red: 1.0, green: 0.61, blue: 0.0, alpha: 1.0)
        calendar.appearance.selectionColor = UIColor(red: 0.35, green: 0.68, blue: 0.90, alpha: 1.0)
        
        calendar.delegate = context.coordinator
        return calendar
    }
    
    func updateUIView(_ uiView: FSCalendar, context: Context) {
        // Do any additional customization
    }
    // Implement FSCalendarDelegate adapter
        func makeCoordinator() -> Coordinator {
            Coordinator(self)
        }
        
        class Coordinator: NSObject, FSCalendarDelegate {
            var parent: MyCalendarView
            
            init(_ parent: MyCalendarView) {
                self.parent = parent
            }
            
            func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
                // Call the onDateSelected closure with the selected date
                self.parent.onDateSelected?(date)
                
            }
        }
}

struct ContentView: View {
    var body: some View {
        MyCalendarView()
            .frame(height: 100)
    }
}


struct MyCalendarView_Previews: PreviewProvider {
    static var previews: some View {
        MyCalendarView()
    }
}
